<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
     /**
     * 控制器基础方法
     */
    public function _initialize()
    {
        //parent::_initialize();
    }

   /*生成验证码*/
    public function verify() {
       $config =    array(    
			 'fontSize'    =>    55,    // 验证码字体大小  
			 'length'      =>    4,     // 验证码位数  
			 'useNoise'    =>    false, // 关闭验证码杂
		 );
		 $Verify = new \Think\Verify($config);
		 $Verify->entry();
    }
	
	// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
	//false表示允许多次验证
	public function check_verify($code, $id = ''){    
		$verify = new \Think\Verify(array('reset' => false));    
		$data = $verify->check($code, $id);
		$this->ajaxReturn($data);  
	}
	
    public function index(){
		 $this->display();
    }
	
	public function login(){
		 $username = I('post.username');
		 $password = I('post.password');
		 $code = I('post.code');
		 $user2 = M("user2");
		 $log = D("user");//实例化用户类->日志方法
		 if(check_verify($code, $id = '')){
		    $where['username'] = $username;
			$where['password'] = md5($password);
			$arr = $user2 ->where($where) ->find();
			if(count($arr)){
			    session('Aid',$arr['id']);
				session('Ausername',$arr['username']);
				session('Aadmintype',$arr['admintype']);
				session('Aemail',$arr['email']);
				
				/*增加登录日志*/
				$log -> addlog(array('username'=>$username,'content'=>1));
				$this->success("登录成功",U('Admin/index/index'));
			}else{
			    $log -> addlog(array('username'=>$username,'content'=>0));
			    $this->success("账号或密码错误",U('Admin/login/index'));
			}
			
		 }else{
		    $this->error("验证码错误");
		 }
		 
    }
	
	public function logout(){
	   session(null); 
	   $this->redirect("/Login/index");
	}
}